package com.sense.kukoo.common.wiget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.sense.kukoo.common.R;

public class MiddleConfigDialog extends Dialog {

    private TextView tv_title;
    private TextView tv_msg;
    private TextView tv_btn_left;
    private TextView tv_btn_right;
    private View root;
    private String leftStr;
    private int leftColor;
    private String rightStr;
    private int rightColor;
    private String titleStr;
    private String msgStr;

    public MiddleConfigDialog(@NonNull Context context) {
        super(context);
    }

    public MiddleConfigDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected MiddleConfigDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getContext().setTheme(R.style.bottom_dialog);
        setContentView(R.layout.common_middle_config_dialog);
        //
        root = findViewById(R.id.root);
        tv_title = findViewById(R.id.tv_title);
        tv_msg = findViewById(R.id.tv_msg);
        tv_btn_left = findViewById(R.id.tv_btn_left);
        tv_btn_right = findViewById(R.id.tv_btn_right);
        //
        tv_title.setText(titleStr);
        tv_msg.setText(msgStr);
        tv_btn_left.setText(leftStr);
        tv_btn_left.setTextColor(leftColor);
        tv_btn_right.setText(rightStr);
        tv_btn_right.setTextColor(rightColor);
        //
        tv_btn_left.setOnClickListener(mOnClickListener);
        tv_btn_right.setOnClickListener(mOnClickListener);
        root.setOnClickListener(mOnClickListener);
    }

    @Override
    public void show() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        super.show();
    }

    private MiddleConfigListener mListener;

    public interface MiddleConfigListener{
        void onLeftClick();
        void onRightClick();
    }

    public MiddleConfigDialog setListener(MiddleConfigListener l){
        mListener = l;
        return this;
    }

    private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();
            if(id == R.id.root){
                dismiss();
            }else if(id == R.id.tv_btn_left){
                if (mListener != null) {
                    mListener.onLeftClick();
                }
            }else if(id == R.id.tv_btn_right){
                if (mListener != null){
                    mListener.onRightClick();
                }
            }
        }
    };

    public MiddleConfigDialog setBtnLeft(String resid, @ColorInt int color){
        leftStr = resid;
        leftColor = color;
        return this;
    }

    public MiddleConfigDialog setBtnRight(String resid, @ColorInt int color){
        rightStr = resid;
        rightColor = color;
        return this;
    }

    public MiddleConfigDialog setTitleStr(String resid){
        titleStr = resid;
        return this;
    }

    public MiddleConfigDialog setMsgStr(String resid){
        msgStr = resid;
        return this;
    }
}
